package com.model.action.teacher;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.model.action.BaseAction;
import com.model.service.admin.AdminService;
import com.model.service.student.StudentService;
import com.model.service.teacher.TeacherService;
import com.model.util.PageList;
import com.model.vo.Admin;
import com.model.vo.Student;
import com.model.vo.Teacher;

@SuppressWarnings("serial")
public class TeacherAction extends BaseAction implements SessionAware{

		/*实体参数 声明
		 **********************************************/
	    Map session;
	    Teacher teacher;   //实体类
		@Autowired
		TeacherService teacherService;  //业务层
		String resultView;   //视图页面
		PageList<Teacher> pageList;
		/*相关方法
		 * ********************************************/
	    @Override
	    public String execute() throws Exception {
	    	if(teacher==null){
	    		teacher = new Teacher();
	    		teacher.setId((Integer) session.get("nameId"));
	    	}
	    	teacher = teacherService.QueryById(Teacher.class, teacher.getId());
	    	if(!session.get("role").equals("4")){ 
	    		resultView ="/admin/teacher/TeacherInfo.jsp";
	    	}else{
	    		resultView ="/admin/teacher/addTeacher.jsp";//管理员
	    	}
	    	return INPUT;
	    }
		/**
		 * 保存
		 * @return
		 */
		public String save(){
			if(teacher!=null){
				teacher.setPassword("123456");
				teacherService.save(teacher);
			}
			 resultView ="/admin/teacher_queryList.action?pageList.pageNo=1";
			return "update";
		}
		/**
		 * 更新
		 * @return
		 */
	    @SuppressWarnings("unchecked")
		public String update(){
	    	if(teacher!=null){
	    		teacherService.update(teacher);
			}
	    	if(!session.get("role").equals("4")){
	    		resultView ="/admin/teacher.action";
	    		session.put("name", teacher.getTeachName());
	    	}else{  //管理员修改
	    		 resultView ="/admin/student_queryList.action?pageList.pageNo=1";
	    	}
			return "update";
		}
	    /**
	     * 删除
	     * @return
	     */
	   public String delete(){
		   if(teacher!=null){
			   teacherService.delete(teacher);
			}
		   resultView ="/admin/teacher_queryList.action?pageList.pageNo=1";
			return "update";
		}
	   /**
	    * 分页查询
	    * @return
	    */
		public String queryList(){
		
	    	if(pageList==null){
	    		pageList = new PageList<Teacher>();	
	    	}
	    	Object[] objects =null;
	    	String hql = "from Teacher "; 
	    	 if(pageList.getStuName()!=null){
	    			 objects = new Object[1];
	    			hql += "where   teachName like ?";
	    			objects[0] = "%"+pageList.getStuName()+"%";
	    		}else if(pageList.getAcademy()!=null){
	    		 objects = new Object[1];
	    			hql += "where   academy like ?";
	    			objects[0] = "%"+pageList.getAcademy()+"%";
	    		}else if(pageList.getGradeClass()!=null){
	    			hql += "where   gradeClass like ?";
	    			 objects = new Object[1];
	    			objects[0] = "%"+pageList.getGradeClass()+"%";
	    		}
	    	pageList.setPageSize(15);
	    		int allrow = teacherService.getAllRowCount(hql, objects);
	    		pageList.setRowCount(allrow);
	    		System.out.println(pageList.getStartOfPage()+","+allrow+","+pageList.getEndRow());
	    		pageList.setDataList(teacherService.queryForPage(
	    		pageList.getStartOfPage(),
	    		pageList.getEndRow(),hql,objects));
			return SUCCESS;
		}
	    
	   
	    /**
	     * 通过Id查询
	     * @return
	     */
	   public String queryById(){
		   if(teacher==null){
			   teacher = new Teacher();
			   teacher.setId((Integer) session.get("nameId"));
			}	
		   teacher = teacherService.QueryById(Teacher.class,teacher.getId());
			return "edit";
          
		}
	   /*相关参数注入
	  	 *********************************************/
		
		public String getResultView() {
			return resultView;
		}
		
		public Teacher getTeacher() {
			return teacher;
		}
		public void setTeacher(Teacher teacher) {
			this.teacher = teacher;
		}
		public TeacherService getTeacherService() {
			return teacherService;
		}
		public void setTeacherService(TeacherService teacherService) {
			this.teacherService = teacherService;
		}
	
		public void setResultView(String resultView) {
			this.resultView = resultView;
		}
		public PageList<Teacher> getPageList() {
			return pageList;
		}
		public void setPageList(PageList<Teacher> pageList) {
			this.pageList = pageList;
		}
		public Map getSession() {
			return session;
		}
		public void setSession(Map session) {
			this.session = session;
		}
		
		
			    
	}
